package com.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.app.*;
import com.app.R;
import com.app.cache.SecondLevelCache;
import com.app.constant.Constants;
import com.app.constant.KeyValue;
import com.app.http.HttpDataLoader;
import com.app.http.HttpDataReqUrl;
import com.app.model.request.GetCheckcodeReq;
import com.app.model.request.RegisterReq;
import com.app.model.response.LoginRes;
import com.app.model.response.ResObj;
import com.app.utils.StringUtil;
import com.app.utils.ToastUtil;
import com.app.view.ClearWriteEditText;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ActivityRegister extends ActivityBaseMenuText {

    @BindView(R.id.etPhone)
    ClearWriteEditText etPhone;
    @BindView(R.id.etSmscode)
    ClearWriteEditText etSmscode;
    @BindView(R.id.etPwd)
    ClearWriteEditText etPwd;
    @BindView(R.id.etPwdCopy)
    ClearWriteEditText etPwdCopy;
    @BindView(R.id.tvSubmit)
    TextView tvSubmit;
    @BindView(R.id.tvSmscode)
    TextView tvSmscode;


    private HttpDataLoader dataLoader;

    private int remainTime = 60;

    private Handler handler = new Handler();


    Runnable runnable = new Runnable() {
        public void run() {
            if (remainTime == 1) {
                tvSmscode.setText("验证码");
                remainTime = 60;
                tvSmscode.setEnabled(true);
                handler.removeCallbacks(runnable);
            } else {
                tvSmscode.setText(String.valueOf(--remainTime));
                handler.postDelayed(runnable, 1000);
            }
            return;

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        setTitle("注册");
        dataLoader = new HttpDataLoader(this);
        handler = new Handler();
    }

    private void register() {
        if (StringUtil.isEmpty(etPhone.getText().toString())) {
            ToastUtil.getInstance().showToast(ActivityRegister.this, "请输入手机号");
            return;
        }
        if (StringUtil.isEmpty(etPwd.getText().toString())) {
            ToastUtil.getInstance().showToast(ActivityRegister.this, "请输入密码");
            return;
        }
        if (StringUtil.isEmpty(etPwdCopy.getText().toString())) {
            ToastUtil.getInstance().showToast(ActivityRegister.this, "请输入确认密码");
            return;
        }
        if (StringUtil.isEmpty(etSmscode.getText().toString())) {
            ToastUtil.getInstance().showToast(ActivityRegister.this, "请输入验证码");
            return;
        }
        if (!etPwd.getText().toString().equals(etPwdCopy.getText().toString())) {
            ToastUtil.getInstance().showToast(ActivityRegister.this, "两次输入密码不一致，请重新输入");
            return;
        }
        RegisterReq req = new RegisterReq();
        req.setPassword(etPwd.getText().toString());
        req.setPhone(etPhone.getText().toString());
        req.setSmscode(etSmscode.getText().toString());
        req.setNeedAuth(false);
        dataLoader.POST(HttpDataReqUrl.URL_REGISTER, req);
        Map<String, String> map_value = new HashMap<String, String>();
        map_value.put("账号" , etPhone.getText().toString());
        map_value.put("密码" , etPwd.getText().toString());
        map_value.put("验证码" , etSmscode.getText().toString());
        MobclickAgent.onEventValue(Application.instance, "register" , map_value, 0);
    }


    private void getCheckCode() {
        if (StringUtil.isEmpty(etPhone.getText().toString())) {
            ToastUtil.getInstance().showToast(ActivityRegister.this, "请输入手机号");
            return;
        }
        handler.post(runnable);
        tvSmscode.setEnabled(false);
        GetCheckcodeReq req = new GetCheckcodeReq();
        req.setPhone(etPhone.getText().toString());
        req.setNeedAuth(false);
        dataLoader.POST(HttpDataReqUrl.URL_GET_SMS_CODE, req);

        Map<String, String> map_value = new HashMap<String, String>();
        map_value.put("账号" , etPhone.getText().toString());
        MobclickAgent.onEventValue(Application.instance, "registerGetCheckCode" , map_value, 0);
    }

    @Override
    public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
        if (url.equals(HttpDataReqUrl.URL_REGISTER)) {
            if (result.getCode() == Constants.CODE_SUCCESS) {

                Map<String, String> map_value = new HashMap<String, String>();
                map_value.put("账号" , etPhone.getText().toString());
                map_value.put("密码" , etPwd.getText().toString());
                map_value.put("验证码" , etSmscode.getText().toString());
                MobclickAgent.onEventValue(Application.instance, "registerSuccess" , map_value, 0);
                if (result.getData() instanceof LoginRes) {
                    LoginRes LoginRes = (LoginRes) (result.getData());
                    SecondLevelCache.sharedInstance().put(KeyValue.TOKEN, LoginRes.getToken());
                    SecondLevelCache.sharedInstance().put(KeyValue.LOGIN_NAME, etPhone.getText().toString());
                    SecondLevelCache.sharedInstance().put(KeyValue.LOGIN_PWD, etPwd.getText().toString());
                    // 注册成功，跳转到首页
                    Intent intent = new Intent(ActivityRegister.this, ActivityGetPwd.class);
                    startActivity(intent);
                    finish();
                } else {
                    ToastUtil.getInstance().showToast(this, result.getMsg());
                }
            } else {
                ToastUtil.getInstance().showToast(this, result.getMsg());
                Map<String, String> map_value = new HashMap<String, String>();
                map_value.put("账号" , etPhone.getText().toString());
                map_value.put("密码" , etPwd.getText().toString());
                map_value.put("验证码" , etSmscode.getText().toString());
                map_value.put("原因" , result.getMsg());
                MobclickAgent.onEventValue(Application.instance, "registerFail" , map_value, 0);

            }
        } else if (url.equals(HttpDataReqUrl.URL_GET_SMS_CODE)) {
            if (result.getCode() == Constants.CODE_SUCCESS) {
                Map<String, String> map_value = new HashMap<String, String>();
                map_value.put("账号" , etPhone.getText().toString());
                map_value.put("验证码" , result.getData().toString());
                MobclickAgent.onEventValue(Application.instance, "registerGetCheckCodeSuccess" , map_value, 0);
//                etSmscode.setText(result.getData().toString());
                ToastUtil.getInstance().showToast(this, result.getMsg());
            } else {
                Map<String, String> map_value = new HashMap<String, String>();
                map_value.put("账号" , etPhone.getText().toString());
                map_value.put("原因" , result.getMsg());
                MobclickAgent.onEventValue(Application.instance, "registerGetCheckCodeFail" , map_value, 0);
                ToastUtil.getInstance().showToast(this, result.getMsg());
            }
        }
    }

    @Override
    public void onRequestError(String url, int statusCode, String error) {
        ToastUtil.getInstance().showToast(this, error);
    }

    @OnClick({R.id.tvSubmit, R.id.tvSmscode, R.id.tvLogin})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvSubmit:
                register();
                break;
            case R.id.tvSmscode:
                getCheckCode();
                break;
            case R.id.tvLogin:
                startActivity(new Intent(ActivityRegister.this, ActivityLogin.class));
                finish();
                break;
//            case R.id.tvAgreement:
//                startActivity(new Intent(ActivityRegister.this, ActivityAgreement.class));
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(0, R.anim.push_left_out);
    }

}
